package array.leetcode.medium;

/**
 * @author bruin_du
 * @description 和大于等于 target 的最短子数组
 * @date 2022/8/3 14:56
 **/
public class OfferII008_MinSubArrayLen {
    //滑动窗口
    public int minSubArrayLen(int target, int[] nums) {
        int res = Integer.MAX_VALUE, left = 0;
        long sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            while (sum >= target && left <= i) {
                res = Math.min(res, i - left + 1);
                sum -= nums[left];
                left++;
            }
        }
        if (res != Integer.MAX_VALUE) return res;
        else return 0;
    }
}
